/*
 * MainEntryPoint.java
 *
 */

package org.wooxes.client;

import com.google.gwt.core.client.EntryPoint;
import com.google.gwt.user.client.History;
import com.google.gwt.user.client.HistoryListener;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.DockPanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.LoadListener;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

import org.wooxes.client.Sink.SinkInfo;

public class Main implements EntryPoint, HistoryListener {
    
    // Declaració dels 3 panels
    private DockPanel panel;
    private VerticalPanel topPanel;
    private VerticalPanel bottomPanel;

    protected SinkList list;
    private SinkInfo curInfo;
    private Sink curSink;
    private HTML description;
    private DockPanel sinkContainer;
    
    public void onHistoryChanged(String token) {
        // Find the SinkInfo associated with the history context. If one is
        // found, show it (It may not be found, for example, when the user mis-
        // types a URL, or on startup, when the first context will be "").
        SinkInfo info = list.find(token);
        if (info == null) {
            showInfo();
            return;
        }
        show(info, false);
    }
    public void onModuleLoad() {
        // Assignació de valor als atributs
        panel = new DockPanel();
        topPanel = createTopPanel();
        //bottomPanel = createBottomPanel();
        list = new SinkList();
        description = new HTML();
        description.setStyleName("ks-Info");
        sinkContainer = new DockPanel();
        sinkContainer.setStyleName("ks-Sink");

        loadSinks();
        // Declaració de variables
        
        VerticalPanel vp = new VerticalPanel();
        vp.setWidth("100%");
        vp.add(description);
        vp.add(sinkContainer);

        panel.add(topPanel, DockPanel.NORTH);
        //panel.add(bottomPanel, DockPanel.SOUTH);
        panel.add(vp, DockPanel.CENTER);
        panel.add(list, DockPanel.WEST);
        
        // Modificació dels aspectes del panel principal
        panel.setCellWidth(vp, "100%");
        panel.setCellVerticalAlignment(list, DockPanel.ALIGN_TOP);

        History.addHistoryListener(this);
        RootPanel.get().add(panel);

        // Show the initial screen.
        String initToken = History.getToken();
        if (initToken.length() > 0) {
          onHistoryChanged(initToken);
        } else {
          showInfo();
        }
    }

    
    private VerticalPanel createTopPanel(){
        VerticalPanel vpHeader = new VerticalPanel();
        vpHeader.setStyleName("Header");
        
        Image imatge = new Image();
        imatge.setUrl("images/logo.jpg");    
        imatge.setStyleName("Header-Imatge");
        
        // En cas que no es carregui l'imatge, es mostrarà el text
        final Label textAlternatiu = new Label("Logo");
        textAlternatiu.setVisible(false);
        
        imatge.addLoadListener(new LoadListener(){
            public void onError(Widget sender) {
                //Si no es carrega l'imatge, es visualitzarà el text
                textAlternatiu.setVisible(true);
            }
            public void onLoad(Widget sender){
            }
        });
        
        Label aboutLink = new Label("Sobre l'aplicació...");
        aboutLink.setStyleName("link");
        aboutLink.addClickListener(new ClickListener(){
            public void onClick(Widget sender) {
                DialogAbout aboutDialog = new DialogAbout();
                aboutDialog.show();
                aboutDialog.center();
            }}
        );

        vpHeader.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER);
        vpHeader.add(imatge);
        vpHeader.add(textAlternatiu);
        vpHeader.setHorizontalAlignment(VerticalPanel.ALIGN_RIGHT);
        
        //Si no s'inserta dins el hp, el click el faria a tota la línia, i no solament al text
        HorizontalPanel hp = new HorizontalPanel();
        hp.add(aboutLink);
        vpHeader.add(hp);

        return vpHeader;
    }
    
    private VerticalPanel createBottomPanel(){
        VerticalPanel vp = new VerticalPanel();
        
        Image imatge = new Image();
        imatge.setUrl("images/email.png");
        imatge.setStyleName("Footer-Imatge");
        
        // En cas que no es carregui l'imatge, es mostrarà el text
        final Label textAlternatiu = new Label("eMail");
        textAlternatiu.setVisible(false);
        
        imatge.addLoadListener(new LoadListener(){
            public void onError(Widget sender) {
                //Si no es carrega l'imatge, es visualitzarà el text
                textAlternatiu.setVisible(true);
            }
            public void onLoad(Widget sender){
            }
        });

        vp.setWidth("100%");
        vp.setHorizontalAlignment(VerticalPanel.ALIGN_CENTER);
        vp.add(imatge);
        vp.add(textAlternatiu);
        
        return vp;
    }
    
    public void show(SinkInfo info, boolean affectHistory) {
        // Don't bother re-displaying the existing sink. This can be an issue
        // in practice, because when the history context is set, our
        // onHistoryChanged() handler will attempt to show the currently-visible
        // sink.
        if (info == curInfo) {
          return;
        }
        if( curInfo != null){
            curInfo.removeInstance();
        }
        
        curInfo = info;

        // Remove the old sink from the display area.
        if (curSink != null) {
          curSink.onHide();
          sinkContainer.remove(curSink);
        }

        // Get the new sink instance, and display its description in the
        // sink list.
        curSink = info.getInstance();
        list.setSinkSelection(info.getName());
        description.setHTML(info.getDescription());

        // If affectHistory is set, create a new item on the history stack. This
        // will ultimately result in onHistoryChanged() being called. It will call
        // show() again, but nothing will happen because it will request the exact
        // same sink we're already showing.
        if (affectHistory) {
          History.newItem(info.getName());
        }

        // Display the new sink.
        sinkContainer.add(curSink, DockPanel.CENTER);
        sinkContainer.setCellWidth(curSink, "100%");
        sinkContainer.setCellHeight(curSink, "100%");
        sinkContainer.setCellVerticalAlignment(curSink, DockPanel.ALIGN_TOP);
        curSink.onShow();
    }

      /**
       * Adds all sinks to the list. Note that this does not create actual instances
       * of all sinks yet (they are created on-demand). This can make a significant
       * difference in startup time.
       */

    protected void loadSinks() {
        list.addSink(Inici.init());
        list.addSink(Activitats.init());
        list.addSink(Administracio.init());

    }

    private void showInfo() {
        show(list.find("Inici"), false);
    }
}
